package com.btpj.lib_base.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.btpj.lib_base.R;

import androidx.annotation.Nullable;

/**
 * Created time  2021/3/25
 * @author : mcx
 * 类描述 : 
 */

public class SoundView extends RelativeLayout {

    private TextView            tvVoiceDuration;
    private LottieAnimationView lavSound;

    public SoundView(Context context) {
        super(context);
    }

    public SoundView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_sound,this);
        tvVoiceDuration = findViewById(R.id.tvVoiceDuration);
//        lavSound = findViewById(R.id.lavSound);
    }

    public void setDuration(String duration) {
        tvVoiceDuration.setText(duration);
    }

    public void playVoice(String url){
        lavSound.playAnimation();
        MediaManager.reset();
        MediaManager.playSound(getContext(), url, mp -> {
            MediaManager.release();
            lavSound.cancelAnimation();
            lavSound.setProgress(1);
            if (mOnCompletionListener!=null) {
                mOnCompletionListener.onCompletion();
            }
        });
    }

    public void cancelVoice(){
        MediaManager.release();
        lavSound.cancelAnimation();
        lavSound.setProgress(1);
    }

    public boolean isStart(){
        return MediaManager.isStart();
    }

    private OnCompletionListener mOnCompletionListener;

    public void setOnCompletionListener(OnCompletionListener listener)
    {
        mOnCompletionListener = listener;
    }

    public interface OnCompletionListener
    {
        /**
         * Called when the end of a media source is reached during playback.
         *
         *
         */
        void onCompletion();
    }
}
